﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblUserAccountDAO
    {
        private DBService _db = null;
        public TblUserAccountDAO(String ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public DataTable UserAccount_InUp(TblUserAccount user, int mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("TblUserAccount_InUp",
                    new string[] { "@AccountID", "@Password", "@Email", "@BusinessName", "@FirstName", "@LastName", "@Suburb", "@State", 
                                "@Country", "@ZipCode", "@MobilePhone", "@HomePhone", "@IsSupperAdmin", "@Newsletter", "@StreetAddress" ,"@Mode"},
                    new object[] { user.AccountID, user.Password, user.Email, user.BusinessName, user.FirstName, user.LastName,
                user.Suburb, user.State, user.Country, user.ZipCode, user.MobilePhone, user.HomePhone, user.IsSupperAdmin, user.Newsletter, user.StreetAddress, mode }, CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        // check Email

        public bool CheckExits(string mail, int mode)
        {
            bool flag = false;
            try
            {
                DataTable dt = _db.ExeSP_DataTable("TblUserAccount_CheckExists",
                    new string[] { "@Email", "@Mode" },
                    new object[] { mail, mode },
                    CommandType.StoredProcedure);
                if (dt.Rows.Count > 0)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        // Get State

        public DataTable GetState_All()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from AUState order by statename ASC", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

      

        // Check login
        public TblUserAccount GetAdmin(string Email, string PassWord)
        {
            TblUserAccount user = new TblUserAccount();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("TblUserAccount_Check",
                   new string[] { "@Email", "@Password" },
                   new object[] { Email, PassWord },
                  CommandType.StoredProcedure);

                foreach (DataRow dr in dt.Rows)
                {
                    user.Email = dr["Email"].ToString();
                    user.AccountID = Convert.ToInt32(dr["AccountID"]);
                    user.State = dr["State"].ToString();
                    user.Suburb = dr["Suburb"].ToString();
                    user.ZipCode = dr["ZipCode"].ToString();
                    user.Country = dr["Country"].ToString();
                    user.BusinessName = dr["BusinessName"].ToString();
                    user.LastName = dr["LastName"].ToString();
                    user.FirstName = dr["FirstName"].ToString();
                    user.Password = dr["Password"].ToString();
                    user.IsSupperAdmin = Convert.ToBoolean(dr["IsSupperAdmin"]);
                    user.HomePhone = dr["HomePhone"].ToString();
                    user.MobilePhone = dr["MobilePhone"].ToString();
                    user.StreetAddress = dr["StreetAddress"].ToString();
                  
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return user;
        }
        // Login Admin

        public TblUserAccount LoginAdmin(string Email, string PassWord)
        {
            TblUserAccount user = new TblUserAccount();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_AdminCheck",
                   new string[] { "@Email", "@Password" },
                   new object[] { Email, PassWord },
                  CommandType.StoredProcedure);

                foreach (DataRow dr in dt.Rows)
                {
                    user.Email = dr["Email"].ToString();
                    user.AccountID = Convert.ToInt32(dr["AccountID"]);
                    user.State = dr["State"].ToString();
                    user.Suburb = dr["Suburb"].ToString();
                    user.ZipCode = dr["ZipCode"].ToString();
                    user.Country = dr["Country"].ToString();
                    user.BusinessName = dr["BusinessName"].ToString();
                    user.LastName = dr["LastName"].ToString();
                    user.FirstName = dr["FirstName"].ToString();
                    user.Password = dr["Password"].ToString();
                    user.IsSupperAdmin = Convert.ToBoolean(dr["IsSupperAdmin"]);
                    user.HomePhone = dr["HomePhone"].ToString();
                    user.MobilePhone = dr["MobilePhone"].ToString();
                    user.StreetAddress = dr["StreetAddress"].ToString();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return user;
        }

        // Load State
        public DataTable GetState()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from AUState order by statename ASC", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // get suburb
        public DataTable GetSuburb_All()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from AUSuburb order by suburb ASC", false, CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetState_BySuburb(string suburb, string postcode)
        {
            DataTable dt = null;
            try
            {
                dt =
                    _db.ExeSP_DataTable(
                        "select AUState.state, statename from AUSuburb, AUState  where postcode = @postcode and suburb =@suburb and AUState.state = AUSuburb.state",
                        new string[] { "@suburb", "@postcode" },
                        new object[] { suburb, postcode },
                        CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        // Get postcode
        public DataTable GetPostcode_ByState(string suburb)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select postcode, suburb, state from AUSuburb where Suburb=@suburb",
                                         new string[] { "@suburb" },
                                         new object[] { suburb }, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
